﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace FSC.HaNamAPApplication
{
    public partial class HNAP_APDetail : System.Web.UI.UserControl
    {
        public string CurrentUrlWithoutParam
        {
            get
            {
                return Controler.UrlWithoutParam(Request.RawUrl);
            }
        }

        private int apid
        {
            get
            {
                int apid = -1;
                if (Request.QueryString["aid"] != null && int.TryParse(Request.QueryString["aid"].ToString(), out apid))
                {
                    apid = int.Parse(Request.QueryString["aid"].ToString());
                }
                return apid;
            }
        }

        protected int number = 1;
        protected int number2 = 1;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            HaNamAPDataDataContext db = new HaNamAPDataDataContext(Controler.HaNamAPConnectionString);
            AdminProcedure objAP = db.AdminProcedures.SingleOrDefault(a => a.AP_ID == this.apid);
            if (objAP != null)
            {
                lblName.Text = objAP.Name;
                lblOffice.Text = objAP.Office.Name;
                lblField.Text = objAP.Field.Name;

                litHowToPerform.Text = objAP.HowToPerform;
                litTimeLimit.Text = objAP.TimeLimitPerform;
                litSubjectPerform.Text = objAP.SubjectsPerform;
                litFee.Text = objAP.Fee;
                litResultPerformAP.Text = objAP.ResultPerformAP;

                litSteps.Text = Server.HtmlDecode(objAP.Steps);
                litCompositionOfRecord.Text = Server.HtmlDecode(objAP.CompositionOfRecord);
                litNumberOfRecord.Text = objAP.NumberOfRecords;
                litRequestAP.Text = Server.HtmlDecode(objAP.RequestAP);

                BindRepeaterAttachFiles();
                BindRepeaterLegal();
            }
        }

        private void BindRepeaterAttachFiles()
        {
            HaNamAPDataDataContext db = new HaNamAPDataDataContext(Controler.HaNamAPConnectionString);
            var data = from a in db.AdminProceduresFiles
                       where a.AP_ID == this.apid
                       select new
                       {
                           a.Name,
                           a.Path
                       };
            rptAttachFiles.DataSource = data;
            rptAttachFiles.DataBind();
        }

        private void BindRepeaterLegal()
        {
            HaNamAPDataDataContext db = new HaNamAPDataDataContext(Controler.HaNamAPConnectionString);
            var data = from a in db.AP_Documents
                       where a.AP_ID == this.apid
                       select new
                       {
                           Name = a.Document.Name,
                           ID = a.Doc_ID
                       };
            rptLegal.DataSource = data;
            rptLegal.DataBind();
        }
    }
}